// SplashScreenState.cpp: implementation of the SplashScreenState class.
//
//////////////////////////////////////////////////////////////////////

#include "SplashScreenState.h"

/* States that can be reached includes */
#include "LoadBriefingState.h"
#include "NewScenarioState.h"
#include "LoadScenarioState.h"
#include "OptionsState.h"
#include "CreditsState.h"
#include "ExitState.h"


#include <stdio.h>
#include <stdlib.h>

#ifdef FREEGLUT_IS_PRESENT
#  include <GL/freeglut.h>
#else
#  ifdef __APPLE__
#    include <GLUT/glut.h>
#  else
#    include <GL/glut.h>
#  endif
#endif

#include "Controller.h"
#include "BMPImage.h"

#include "ImageButton.h"
#include "BackGroundImage.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SplashScreenState::SplashScreenState()
{
glClearColor(0.0f,1.0f,0.0f,1.0f);

exitButton = new ImageButton("./exitNormal.bmp",
                             "./exitFocused.bmp",
                             "./exitClicked.bmp",
                             eAlphaKey_RED,
                             0.84f,0.01f,0.15f,0.1f);
             
creditsButton = new ImageButton("./creditsNormal.bmp",
                             "./creditsFocused.bmp",
                             "./creditsClicked.bmp",
                             eAlphaKey_RED,
                             0.01f,0.01f,0.15f,0.1f);

createNewScenarioButton = new ImageButton("./createNewScenarioNormal.bmp",
                             "./createNewScenarioFocused.bmp",
                             "./createNewScenarioClicked.bmp",
                             eAlphaKey_RED,
                             0.15f,0.3f,0.15f,0.15f);
                             
loadScenarioButton = new ImageButton("./loadScenarioNormal.bmp",
                             "./loadScenarioFocused.bmp",
                             "./loadScenarioClicked.bmp",
                             eAlphaKey_RED,
                             0.32f,0.12f,0.15f,0.15f);
  
optionsButton = new ImageButton("./optionsNormal.bmp",
                             "./optionsFocused.bmp",
                             "./optionsClicked.bmp",
                             eAlphaKey_RED,
                             0.52f,0.12f,0.15f,0.15f); 

                            
loadBriefingButton = new ImageButton("./loadBriefingNormal.bmp",
                             "./loadBriefingFocused.bmp",
                             "./loadBriefingClicked.bmp",
                             eAlphaKey_RED,
                             0.65f,0.3f,0.15f,0.15f);

//bmp = new BMPImage(0.02,0.02,0.3,0.3);
//bmp->load("./GUI/Widgets/crissolo.bmp");

this->backgroundImage = new BackGroundImage("./SplashScreenState.bmp");

this->kb = new Keyboard();
this->ms = new Mouse();

this->nextState = this;

for(int i = 0; i < NEXT_POSSIBLE_STATES; i++)
        {
        this->nextStates[i] = NULL;
        }
}

SplashScreenState::~SplashScreenState()
{
/* delete buttons*/
delete exitButton;
delete creditsButton;
delete optionsButton;
delete loadScenarioButton;
delete createNewScenarioButton;
delete loadBriefingButton;
/* delete controllers */
delete kb;
delete ms;
/* delete background image */
delete backgroundImage;
/* delete map */
//delete bmp;
}

void SplashScreenState::load(const char* filename)
{
	
}

void SplashScreenState::display(void)
{
this->backgroundImage->display();
//bmp->display();

// buttons
exitButton->display();
creditsButton->display();
optionsButton->display();
loadScenarioButton->display();
createNewScenarioButton->display();
loadBriefingButton->display();
}

void SplashScreenState::resize(int w, int h)
{

}

void SplashScreenState::keyUp(unsigned char key, int x, int y)
{
kb->read(key,GLUT_UP,x,y);
}

void SplashScreenState::keyDown(unsigned char key, int x, int y)
{
kb->read(key,GLUT_DOWN,x,y);
}

void SplashScreenState::specialUp(int key, int x, int y)
{
kb->read(key,GLUT_UP,x,y);
}

void SplashScreenState::specialDown(int key, int x, int y)
{
kb->read(key,GLUT_DOWN,x,y);
//printf("%d %d %d\n",key,PW_DOWN,GLUT_DOWN);
}

void SplashScreenState::motion(int newx, int newy)
{
	exitButton->mouse(-1,-1,newx,newy);
	creditsButton->mouse(-1,-1,newx,newy);
	optionsButton->mouse(-1,-1,newx,newy);
	loadScenarioButton->mouse(-1,-1,newx,newy);
	createNewScenarioButton->mouse(-1,-1,newx,newy);
	loadBriefingButton->mouse(-1,-1,newx,newy);
}

void SplashScreenState::passive(int newx, int newy)
{
	exitButton->mouse(-1,-1,newx,newy);
	creditsButton->mouse(-1,-1,newx,newy);
	optionsButton->mouse(-1,-1,newx,newy);
	loadScenarioButton->mouse(-1,-1,newx,newy);
	createNewScenarioButton->mouse(-1,-1,newx,newy);
	loadBriefingButton->mouse(-1,-1,newx,newy);
}

void SplashScreenState::mouse(int button, int state, int mouseX, int mouseY)
{
//bmp->mouse(button,state,mouseX,mouseY);
if(exitButton->mouse(button,state,mouseX,mouseY))
    {
    exit(0);
    /* EXIT BUTTON PRESSED, so EXIT*/
    }
else if(loadBriefingButton->mouse(button,state,mouseX,mouseY))
    {
    if(this->nextStates[LOAD_BRIEFING_STATE_NUMBER] != NULL) // Already created, just use it!
			{
			this->nextState = this->nextStates[LOAD_BRIEFING_STATE_NUMBER];
			LoadBriefingState *lss = (LoadBriefingState*)this->nextState;
			lss->reopen();
			}
    else 
         {
         this->nextStates[LOAD_BRIEFING_STATE_NUMBER] = (State*) new LoadBriefingState((State*)this);    
         this->nextState = this->nextStates[LOAD_BRIEFING_STATE_NUMBER];
         }
    }
else if(optionsButton->mouse(button,state,mouseX,mouseY))
    {
    if(this->nextStates[OPTIONS_STATE_NUMBER] != NULL) // Already created, just use it!
              this->nextState = this->nextStates[OPTIONS_STATE_NUMBER];
    else 
         {
         this->nextStates[OPTIONS_STATE_NUMBER] = (State*) new OptionsState((State*)this);    
         this->nextState = this->nextStates[OPTIONS_STATE_NUMBER];
         }
    }
else if(creditsButton->mouse(button,state,mouseX,mouseY))
    {
    if(this->nextStates[CREDITS_STATE_NUMBER] != NULL) // Already created, just use it!
              this->nextState = this->nextStates[CREDITS_STATE_NUMBER];
    else 
         {
         this->nextStates[CREDITS_STATE_NUMBER] = (State*) new CreditsState((State*)this);    
         this->nextState = this->nextStates[CREDITS_STATE_NUMBER];
         }
    }
else if(createNewScenarioButton->mouse(button,state,mouseX,mouseY))
    {
    if(this->nextStates[NEW_SCENARIO_STATE_NUMBER] != NULL) // Already created, just use it!
			{
            this->nextState = this->nextStates[NEW_SCENARIO_STATE_NUMBER];
			NewScenarioState *nss = (NewScenarioState*)this->nextState;
			nss->reopen();
			}
    else 
         {
         this->nextStates[NEW_SCENARIO_STATE_NUMBER] = (State*) new NewScenarioState((State*)this);    
         this->nextState = this->nextStates[NEW_SCENARIO_STATE_NUMBER];
         }
    }        
else if(loadScenarioButton->mouse(button,state,mouseX,mouseY))
    {
    if(this->nextStates[LOAD_SCENARIO_STATE_NUMBER] != NULL) // Already created, just use it!
			{  
			this->nextState = this->nextStates[LOAD_SCENARIO_STATE_NUMBER];
			LoadScenarioState *lss = (LoadScenarioState*)this->nextState;
			lss->reopen();
			}
    else 
         {
         this->nextStates[LOAD_SCENARIO_STATE_NUMBER] = (State*) new LoadScenarioState((State*)this);    
         this->nextState = this->nextStates[LOAD_SCENARIO_STATE_NUMBER];
         }
    } 
}

State* SplashScreenState::idle(void)
{
//if(kb->isPressed(GLUT_KEY_LEFT))       bmp->moveRight(0);
//if(kb->isPressed(GLUT_KEY_RIGHT))      bmp->moveLeft(0);
//if(kb->isPressed(GLUT_KEY_UP))         bmp->moveDown(0);
//if(kb->isPressed(GLUT_KEY_DOWN))       bmp->moveUp(0);
//if(kb->isPressed(GLUT_KEY_PAGE_UP))    bmp->zoomOut(0);
//if(kb->isPressed(GLUT_KEY_PAGE_DOWN))  bmp->zoomIn(0);
if(kb->isPressed(27)) exit(0);
if(nextState == this) return nextState;
else {State* tmpState = nextState; nextState=this; return tmpState;}
}

